class Observer {
    constructor(data) {
        this.walk(data);
    }

    walk(data) {
        if (Utils.is2type(data, "object")) {
            for (const key of Object.keys(data)) {
                const value = data[key];
                this.walk(value);
                this.defineReactive(data, key, value);
            }
        }
    }

    /**
     * 
     * @param {object} data 
     * @param {string} key 
     * @param {any} value 
     */
    defineReactive(data, key, value) {
        const self = this;
        const dep = new Dep(); // Dep与Vue参数值:data属性(名/值)一一对应
        Object.defineProperty(data, key, {
            enumerable: true,
            configurable: true,
            set(newVal) {
                if (newVal !== value) {
                    value = newVal;
                    // if (Utils.is2type(value, "object")) {
                    //     self.walk(value);
                    // }
                    dep.notify();
                }
            },
            get() {
                if (Dep.target) dep.addSub(Dep.target);
                // console.log(key, dep);
                return value; // 避免重复调用getter函数
            }
        })
    }
}